package com.witmore.skyline.web.constants;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.witmore.skyline.db.entity.CameraDO;
import com.witmore.skyline.db.entity.CameraPicDO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TableSortConstant {
    /**
     * 允许排序的字段
     */
    public static final Set<String> DEVICE_ALLOWED_SORT_FIELDS = Set.of("create_time", "work_status", "oil_level", "today_work_time", "work_status_custom");

    public static final Set<String> ALLOWED_SORT_ORDER = Set.of("ASC", "DESC");

    private static final Map<String, SFunction<CameraPicDO, ?>> FIELD_MAPPING_CAMERA_PIC = new HashMap<>();

    private static final Map<String, SFunction<CameraDO, ?>> FIELD_MAPPING_CAMERA = new HashMap<>();
    static {
        // 摄像头图片表
        FIELD_MAPPING_CAMERA_PIC.put("createTime", CameraPicDO::getCreateTime);

        // 摄像头表
        FIELD_MAPPING_CAMERA.put("cameraStatus", CameraDO::getCameraStatus);
        FIELD_MAPPING_CAMERA.put("createTime", CameraDO::getCreateTime);



    }
    // ------------------------------------------------ skyline_camera_pic 表搜索字段---------------------------------------------
    public static SFunction<CameraPicDO, ?> convertToDbFieldCameraPic(String camelPicField) {
        if (StringUtils.isEmpty(camelPicField)){
            return FIELD_MAPPING_CAMERA_PIC.get("createTime");
        }
        SFunction<CameraPicDO, ?> cameraPicDOSFunction = FIELD_MAPPING_CAMERA_PIC.get(camelPicField);
        if (ObjectUtils.isEmpty(cameraPicDOSFunction)){
            return FIELD_MAPPING_CAMERA_PIC.get("createTime");
        }
        return cameraPicDOSFunction;
    }
    // ------------------------------------------------ skyline_camera_pic 表搜索字段---------------------------------------------





    // ------------------------------------------------ skyline_camera 表搜索字段---------------------------------------------
    public static void convertToDbFieldCamera(LambdaQueryWrapper<CameraDO> queryWrapper, String camelSortField, String cameraSortOrder) {
        if (StringUtils.isNotEmpty(camelSortField) && StringUtils.isNotEmpty(cameraSortOrder)) {
            SFunction<CameraDO, ?> cameraDOSFunction = convertToDbFieldCamera(camelSortField);
            if (cameraSortOrder.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(cameraDOSFunction);
            } else {
                queryWrapper.orderByDesc(cameraDOSFunction);
            }
        } else {
            queryWrapper.orderByDesc(CameraDO::getCreateTime);
        }
    }

    public static SFunction<CameraDO, ?> convertToDbFieldCamera(String camelSortField) {
        if (StringUtils.isEmpty(camelSortField)){
            return FIELD_MAPPING_CAMERA.get("createTime");
        }
        SFunction<CameraDO, ?> cameraDOSFunction = FIELD_MAPPING_CAMERA.get(camelSortField);
        if (ObjectUtils.isEmpty(cameraDOSFunction)){
            return FIELD_MAPPING_CAMERA.get("createTime");
        }
        return cameraDOSFunction;
    }
    // ------------------------------------------------ skyline_camera 表搜索字段---------------------------------------------

}
